﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class DocSignerProcessBll
    {
        public static DocSignerCls CreateDocSignerModel(string SiteId, string DocSignerId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocSigner " +
                                               "     where DocSignerId=@DocSignerId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("DocSignerId", DocSignerId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            DocSignerCls ODocSigner = DocSignerParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ODocSigner;
        }


        public DocSignerCls[] GetDocSingerByGroupReleaseFrom(string SiteId, string SelectDocReleaseFrom)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);

            string sqlQuery = string.Empty;
            if (!string.IsNullOrEmpty(SelectDocReleaseFrom))
                //sqlQuery = "select * from TableDocSigner where frkDocReleaseFromId='" + SelectDocReleaseFrom + "' order by DocSignerName";
                sqlQuery = "select * from TableDocSigner where frkDocReleaseFromId=@frkDocReleaseFromId order by DocSignerName";
            else
                sqlQuery = "select * from TableDocSigner where 1=1 order by DocSignerName";

            DataSet dsResult =
                    DBService.GetDataSet(null, sqlQuery, new DbParam[] { new DbParam("frkDocReleaseFromId", SelectDocReleaseFrom) });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new DocSignerCls[0];
            }
            DocSignerCls[] DocSigner = DocSignerParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return DocSigner;
        }

        public static DocSignerCls[] ReadingDocSigner(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocSigner where frkGroupDataId=@frkGroupDataId order by SortIndex",
                    new DbParam[] { new DbParam("frkGroupDataId", GroupDataId) });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new DocSignerCls[0];
            }
            DocSignerCls[] DocSigner = DocSignerParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return DocSigner;
        }

        //SiteId, GroupDataId, Subject, JobTitle, ReleaseDepartment
        public static void AddDocSigner(string SiteId, string GroupDataId, string DocSignerName, string JobTitle, string ReleaseDepartment)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DocSignerId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Insert(null, "TableDocSigner",
                    new DbParam[] 
                    { 
                        new DbParam("DocSignerId", DocSignerId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("frkDocReleaseFromId", ReleaseDepartment),
                        new DbParam("DocSignerName", DocSignerName),
                        new DbParam("JobTitle", JobTitle),
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("DataLanguage", DataLanguage),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Thêm mới người ký " + DocSignerName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void SaveDocSigner(string SiteId, string DocSignerId, string DocSignerName, string JobTitle, string ReleaseDocFromId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableDocSigner", "DocSignerId", DocSignerId,
                    new DbParam[] 
                    { 
                        new DbParam("DocSignerName", DocSignerName),
                        new DbParam("JobTitle", JobTitle),
                        new DbParam("frkDocReleaseFromId", ReleaseDocFromId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Sửa chữa người ký " + DocSignerName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void DeleteDocSigner(string SiteId, string DocSignerId)
        {
            DocSignerCls ODocSigner = DocSignerProcessBll.CreateDocSignerModel(SiteId, DocSignerId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDocSigner where DocSignerId=@DocSignerId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("DocSignerId", DocSignerId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Xóa người ký " + ODocSigner.DocSignerName, 
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName, 
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }
    }
}
